<?php
/**
 * Created by PhpStorm.
 * User: Frings
 * Date: 2018/2/11
 * Time: 0:30
 */

$this->title = '商品列表';
$web = \yii\helpers\Url::base();
?>


<div class="main-title">
    <div class="title-txt"><?= $this->title ?></div>
</div>

<!--search-->
<div class="panel panel-frame panel-operat">
    <div class="panel-body">
        <div class="panel-content">
            <input type="text" class="form-control search-box" placeholder="名称" id="name" name="name"
                   value="<?= $name ?>">
            <a href="javascript:search()" type="button" class="btn btn-warning search-icon">
                <div class="btn-icon"></div>
                <div class="btn-text">搜索</div>
            </a>
            <a href="javascript:editData()" type="button" class="btn btn-primary add-icon ">
                <div class="btn-icon"></div>
                <div class="btn-text">添加</div>
            </a>
        </div>
    </div>
</div>


<!--list-->
<div class="panel panel-frame">
    <div class="table-responsive">
        <table class="table table-bordered table-hover">
            <thead>
            <tr>
                <th>商品</th>
                <th>图片</th>
                <th>价格</th>
                <th>库存</th>
                <th>销量</th>
                <th>状态</th>
                <th>排序</th>
                <th>操作</th>
            </tr>
            </thead>
            <tbody>
            <?php
            for ($i = 0; $i < count($items); $i++) {
                $item = $items[$i];
                ?>
                <tr>
                    <td><?= $item['name'] ?></td>
                    <td class="td-img">
<!--                        --><?//=$item['small_image_url']?>
                        <?php if (!empty($item['small_image_url'])) {
                            ?>
                            <img src="<?=\app\models\File::getFile($item['small_image_url'])['url'] ?>"  class="img-small" style="height: 50px;">
                            <?php
                        }
                        ?>
                    </td>
                    <td><?= $item['selling_price'] ?></td>
                    <td><?= $item['stock'] ?></td>
                    <td><?= $item['sell_count'] ?></td>

                    <td><span class="label label-<?= $item['is_shelved'] == 1 ? 'success' : 'danger' ?>">
                            <?= $item['is_shelved'] == 1 ? '已上架' : '已下架' ?>
                        </span></td>
                    <td><?= $item['sort'] ?></td>


                    <td class="table-text-align-left">
                        <a class="btn btn-link" href="javascript:editData('<?= $item['id'] ?>')">编辑</a>
                        <a class="btn btn-link" href="javascript:deleteData('<?= $item['id'] ?>')">删除</a>
                    </td>
                </tr>
                <?php
            }
            ?>
            </tbody>
        </table>
    </div>
    <div class="table-pagination">
        <?= \app\widgets\LinkPagerExt::widget(['pagination' => $pagination]) ?>
    </div>
</div>


<!--edit-->
<div class="modal fade" id="dataModalEdit" tabindex="-1" role="dialog"
     aria-labelledby="dataModalEditLabel">
    <div class="modal-dialog modal-lg" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title" id="dataModalEditLabel"><?= $this->title . '编辑' ?></h4>
            </div>
            <div id="modalEdit" class="modal-body">
            </div>
            <div class="modal-footer">
                <a class="btn btn-primary" onclick="saveData()"><i class="fa fa-save"></i> 保存
                </a>
                <a class="btn btn-cancel" data-dismiss="modal"><i class="fa fa-remove"></i> 取消
                </a>
            </div>
        </div>
    </div>
</div>


<script type="text/javascript">

    function search() {
        var name = $("#name").val();
        var href = window.location.href;
        var index = href.indexOf("?page=");
        if (index > 0) {
            href = href.substr(0, index);
        }
        var href_str = href + "?page=1";

        if (name != '' && name != null) {
            href_str = href_str + "&name=" + name;
        }
        window.location.href = href_str;
    }

    function editData(id) {

        $('#dataModalEdit').modal({
            keyboard: false, backdrop: 'static', show: false
        });

        $("#dataModalEdit").modal('show');
        $("#dataModalEdit").on('show');
        var data = {};
        if (id != undefined) {
            data.id = id;
        }

        $("#dataModalEdit").find("#modalEdit").html('正在加载数据...');

        requestHtml('goods-partial', data, true, function (responseHtml) {
            $("#dataModalEdit").find("#modalEdit").html(responseHtml);
        });
    }

    var small_img_path;
    function setSmallImgPath(path) {
        small_img_path = path;
    }

    var pro_img_paths = [];
    function setProImgPath(path) {
        pro_img_paths.push(path);
    }

    var detail_img_paths = [];
    function setDetailImgPath(path) {
        detail_img_paths.push(path);
    }
    function saveData() {
//        alert('hahah');

        var id = $("#data_form").find("#id").val();
        var name = $("#data_form").find("#name").val().trim();
        var summary = $("#data_form").find("#summary").val().trim();
        var unit = $("#data_form").find("#unit").val().trim();
        var stock = $("#data_form").find("#stock").val().trim();
        var settlement_price = $("#data_form").find("#settlement_price").val().trim();
        var selling_price = $("#data_form").find("#selling_price").val().trim();
        var old_price = $("#data_form").find("#old_price").val().trim();
        var is_enabled = $("#data_form").find("#is_enabled").val().trim();
        var is_shelved = $("#data_form").find("#is_shelved").val().trim();
        var is_recommend= $("#data_form").find("#is_recommend").val().trim();
        var is_newest = $("#data_form").find("#is_newest").val().trim();
        var is_hot = $("#data_form").find("#is_hot").val().trim();
        var is_free_express = $("#data_form").find("#is_free_express").val().trim();
        var is_promotion = $("#data_form").find("#is_promotion").val().trim();
        var sell_count = $("#data_form").find("#sell_count").val().trim();
        var collected_count = $("#data_form").find("#collected_count").val().trim();
        var express_money = $("#data_form").find("#express_money").val().trim();
        var is_invoice = $("#data_form").find("#is_invoice").val().trim();
        var is_GGCT = $("#data_form").find("#is_GGCT").val().trim();
        var is_seven_day = $("#data_form").find("#is_seven_day").val().trim();
        var is_guarantee = $("#data_form").find("#is_guarantee").val().trim();
        var is_return_goods = $("#data_form").find("#is_return_goods").val().trim();
        var sort = $("#data_form").find("#sort").val().trim();

//        var is_enabled = 0;
//        if ($("#data_form").find("#is_enabled").is(':checked')) {
//            is_enabled = 1;
//        }

        if (name == '' || name == null) {
            warningDialog('名称不能为空');
            return false;
        }

        if (stock == '' || stock == null) {
            warningDialog('库存不能为空');
            return false;
        }

        if (selling_price == '' || selling_price == null) {
            warningDialog('销售价不能为空');
            return false;
        }

        var data = {};
        data.id = id;
        data.name = name;
        data.summary = summary;
        data.unit = unit;
        data.stock = stock;
        data.settlement_price = settlement_price;
        data.selling_price = selling_price;
        data.old_price = old_price;
        data.is_enabled = is_enabled;
        data.is_shelved = is_shelved;
        data.is_recommend = is_recommend;
        data.is_newest = is_newest;
        data.is_hot = is_hot;
        data.is_free_express = is_free_express;
        data.is_promotion = is_promotion;
        data.sell_count = sell_count;
        data.collected_count = collected_count;
        data.express_money = express_money;
        data.is_invoice = is_invoice;
        data.is_GGCT = is_GGCT;
        data.is_seven_day = is_seven_day;
        data.is_guarantee = is_guarantee;
        data.is_return_goods = is_return_goods;
        data.sort = sort;

        data.small_img_path = small_img_path;
        data.pro_img_paths = pro_img_paths;
        data.detail_img_paths = detail_img_paths;

        requestJson("goods-save-handler", data, true, function (response) {
            if (response.status == 0) {
                search();
            } else {
                errorDialog(response.message);
            }
        });
    }

    function deleteData(id) {
        confirmDialog(id, null, function (id) {
            var data = {};
            data.id = id;
            requestJson("goods-delete-handler", data, true, function (response) {
                if (response.status == 0) {
                    search();
                } else {
                    errorDialog(response.message);
                }
            });
        });
    }

</script>
